/*
  1获取用户收货地址
    1.绑定点击事件
    2.调用小程序内置api，获取用户收货地址
*/
// import {getSetting,chooseAddress,openSetting,showModal,showToast} from '../../utils/asyncWx.js'
import {showToast,requestPayment} from '../../utils/asyncWx.js'
import { requests} from '../../request/index.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Address:{},
    cart:[],
 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let Address = wx.getStorageSync('Address')
    let cart = wx.getStorageSync('cart')||[]
   cart= cart.filter(v=>v.checked)
    // let checkout = cart.length?cart.every(v=>v.checked):false
    this.setData({
      Address
    })
    this.bindClick(cart)
  },
  bindItemClick(e){
    // 获取勾选商品id
    var goods_id = e.currentTarget.dataset.id
    // 找到商品
    let {cart} = this.data
   let index= cart.findIndex(v=>v.goods_id===goods_id)
    cart[index].checked = !cart[index].checked
    this.bindClick(cart)
  },

  // 封装勾选方法
  bindClick(cart){
     let cartPrice=0
     let cartNum=0
     cart.forEach(v=>{
     
       cartPrice+=v.num*v.goods_price
       cartNum+=v.num
      
     })
   
     this.setData({
       cart,
       cartPrice,
       cartNum
     })
  },
 
  // 商品结算
async bindOrderPay(){
  try {
    
   const token = wx.getStorageSync('token')
   if(!token){
    wx.navigateTo({
      url: '/pages/auth/index',
    })
    return

   }
   else{
     console.log("已存在token");
     
   }
  // 1.创建订单
  // 1.1 准备 请求头参数
  //const header = {Authorization:token};
  // 1.2 准备 请求体参数
  const order_price = this.data.cartPrice
  const consignee_addr = this.data.Address.all
  const cart = this.data.cart
  let goods = []
  cart.forEach(v=>goods.push({
    goods_id:v.goods_id,
    goods_number:v.num,
    goods_price:v.goods_price
  }))
  const goodPamrs = {order_price,consignee_addr,goods}
  // 生成订单编号
  const {order_number} = await requests({url:"/my/orders/create",method:"POST",data:goodPamrs})
  // 发起预支付接口
  const {pay} = await requests({url:"/my/orders/req_unifiedorder",method:"POST",data:{order_number}})
  await requestPayment(pay)
  console.log("222");
  
  // 查询后台 订单状态
  const res = await requests({url:"/my/orders/chkOrder",method:"POST",data:{order_number}})
  // 支付成功后刪除已付款的购物车商品
  let NewCore = wx.getStorageSync('cart')
  NewCore = NewCore.filter( v=>!v.checked)
  wx.setStorageSync('cart', NewCore)

  await showToast({title:"支付成功"})
  wx.navigateTo({
    url: '/pages/order/index',
  })
  // 支付成功
  } catch (error) {
    await showToast({title:"支付失败"})
  }
  // console.log(order_number);
  
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})